package com.shop.manager.controller;

//import com.shop.manager.entity.TbAttribute;
//import com.shop.manager.http.HttpResult;
//import com.shop.manager.http.PageResult;
import com.base.common.entity.TbAttribute;
import com.base.common.http.HttpResult;
import com.base.common.http.PageResult;
import com.shop.manager.service.AttrService;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 *  @Description 扩展属性信息增删改查
 */

@RestController
@RequestMapping("/attr")
public class AttrController {

    /**
     * @Description 注入service对象
     */
    @Resource(type = AttrService.class)
    private AttrService attrService;

    /**
     * @Description 添加扩展属性信息
     * @param attr 属性对象
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Integer insertAttr(@RequestBody @ApiParam(value = "TbAttribute 对象") TbAttribute attr) {
        try {
            attrService.add(attr);
            return 1;
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * @Description 批量删除扩展属性信息
     * @param ids 扩展属性id的数组
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public HttpResult delete(@RequestBody Integer[] ids) {
        try {
            attrService.deleteByIds(ids);
            //删除成功
            return HttpResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            //删除异常
            return HttpResult.error();
        }
    }

    /**
     * @Description 修改扩展属性信息
     * @param attr 修改的扩展属性对象
     * @return
     */
    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    public Integer updateAttr(@RequestBody @ApiParam(value = "TbAttribute 对象") TbAttribute attr) {
        try {
            attrService.update(attr);
            return 1;
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * @Description 根据id查询扩展属性信息
     * @param id 扩展属性id
     * @return
     */
    @RequestMapping(value = "/queryById", method = RequestMethod.GET)
    public TbAttribute findAttrById(@RequestParam("id") Integer id) {
        return attrService.findOne(id);
    }

    /**
     * @Description 条件分页查询
     * @param pageNum 当前页码
     * @param size 当前页条数
     * @param attr 查询条件对象
     * @return
     */
    @RequestMapping(value = "/getAll", method = RequestMethod.POST)
    public HttpResult getAll(@RequestParam("pageNum") Integer pageNum, @RequestParam("size") Integer size, @RequestBody(required = false) @ApiParam(value = "分页条件Area对象") TbAttribute attr) {
        try {
            //分页查询
            PageResult result = attrService.findPage(pageNum, size, attr);
            //查询成功
            return HttpResult.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            //查询异常
            return HttpResult.error("查询扩展属性异常");
        }
    }

    /**
     * @Description 分页查询
     * @param page 当前页码
     * @param rows 当前页条数
     * @return
     */
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public PageResult findAllAttr(@RequestParam("page") Integer page, @RequestParam("rows") Integer rows) {
        PageResult pageResult = attrService.findPage(page, rows);
        return pageResult;
    }

    /**
     * @Description 模糊查询扩展属性信息
     * @param tbAttribute 扩展属性对象
     * @return
     */
    @RequestMapping(value = "/findByWhere", method = RequestMethod.POST)
    public HttpResult findByWhere(@RequestBody(required = false) @ApiParam(value = "TbAttribute 对象") TbAttribute tbAttribute) {
        return HttpResult.ok(attrService.findByWhere(tbAttribute));
    }







}
